// https://umijs.org/config/
import { defineConfig } from 'umi';
import { join } from 'path';

import defaultSettings from './defaultSettings';
import proxy from './proxy';
import routes from './routes';

const { REACT_APP_ENV } = process.env;
let chainWebpack = function (config, { webpack }) {
  if (process.env.NODE_ENV == "development") {
    return;
  }
  const assetDir = "resource";
  config.output
    .filename(assetDir + '/js/[name].[hash:8].js')
    .chunkFilename(assetDir + '/js/[name].[contenthash:8].chunk.js')

  // 修改css输出目录
  config.plugin("extract-css").tap(() => [
    {
      filename: `${assetDir}/css/[name].[contenthash:8].css`,
      chunkFilename: `${assetDir}/css/[name].[contenthash:8].chunk.css`,
      ignoreOrder: true,
    },
  ]);

  // 修改图片输出目录
  config.module
    .rule("images")
    .test(/\.(png|jpe?g|gif|webp|ico)(\?.*)?$/)
    .use("url-loader")
    .loader(require.resolve("url-loader"))
    .tap((options) => {
      const newOptions = {
        ...options,
        name: assetDir + "/img/[name].[hash:8].[ext]",
        fallback: {
          ...options.fallback,
          options: {
            name: assetDir + "/img/[name].[hash:8].[ext]",
            esModule: false,
          },
        },
      };
      return newOptions;
    });
}

let config = defineConfig({
  publicPath: "./",
  history: { type: "hash" },
  hash: true,
  antd: {
    dark: false
  },
  dva: {
    hmr: true,
  },
  layout: {
    // https://umijs.org/zh-CN/plugins/plugin-layout
    locale: false,
    siderWidth: 208,
    ...defaultSettings,
  },
  // https://umijs.org/zh-CN/plugins/plugin-locale
  locale: {
    // default zh-CN
    default: 'zh-CN',
    antd: false,
    // default true, when it is true, will use `navigator.language` overwrite default
    baseNavigator: false,
  },
  dynamicImport: {
    loading: '@ant-design/pro-layout/es/PageLoading',
  },
  targets: {
    ie: 11,
  },
  // umi routes: https://umijs.org/docs/routing
  routes,
  // Theme for antd: https://ant.design/docs/react/customize-theme-cn
  theme: {
    'primary-color': defaultSettings.primaryColor,
  },
  // esbuild is father build tools
  // https://umijs.org/plugins/plugin-esbuild
  esbuild: {},
  title: false,
  ignoreMomentLocale: true,
  proxy: proxy[REACT_APP_ENV || 'dev'],
  manifest: {
    basePath: '/',
  },
  // Fast Refresh 热更新
  fastRefresh: {},
  openAPI: [
    {
      requestLibPath: "import { request } from 'umi'",
      // 或者使用在线的版本
      // schemaPath: "https://gw.alipayobjects.com/os/antfincdn/M%24jrzTTYJN/oneapi.json"
      schemaPath: join(__dirname, 'oneapi.json'),
      mock: false,
    },
    {
      requestLibPath: "import { request } from 'umi'",
      schemaPath: 'https://gw.alipayobjects.com/os/antfincdn/CA1dOm%2631B/openapi.json',
      projectName: 'swagger',
    },
  ],
  nodeModulesTransform: { type: 'none' },
  mfsu: {}, //false
  webpack5: {},
  // exportStatic: {},
  chainWebpack
});

export default config
